package com.ycy.smartcourseselection.utils;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ycy.smartcourseselection.common.PageParamsDTO;
import com.ycy.smartcourseselection.common.PageResVO;
import com.ycy.smartcourseselection.common.ResCodeEnum;
import com.ycy.smartcourseselection.exception.CustomException;
import com.ycy.smartcourseselection.vo.score.ScoreVO;

import java.util.ArrayList;

public class PageUtils<E,V> {
    /***
     * 功能: 分页查询  添加分页排序条件和其他搜索条件 (需要传一个queryWrapper，可在传之前先构造其他条件)
     * @param pageParamsDTO 分页参数DTO
     * @return 类型：QueryWrapper<E> 对应实体类的 queryWrapper
     */
    public QueryWrapper<E> buildPageQueryWrapper(PageParamsDTO pageParamsDTO, QueryWrapper<E> queryWrapper) {
        try {
            //根据排序字段和排序类型添加排序条件
            String sortField = pageParamsDTO.getSortField();
            String sortType = pageParamsDTO.getSortType();
            if(StrUtil.isBlank(sortType) || "desc".equals(sortType)) {
                if(StrUtil.isBlank(sortField)){
                    queryWrapper.orderByDesc("create_time");
                }else {
                    queryWrapper.orderByDesc(StrUtil.toUnderlineCase(sortField));
                }
            }
            //升序时
            else {
                if(StrUtil.isBlank(sortField)){
                    queryWrapper.orderByAsc("create_time");
                }else {
                    queryWrapper.orderByAsc(StrUtil.toUnderlineCase(sortField));
                }
            }

            return queryWrapper;
        }catch (Exception e) {
            throw new CustomException(ResCodeEnum.BAD_REQUEST,"排序字段错误");
        }
    }

    /***
     * 功能: 分页查询  构造ConditionResVO
     * @param resPage page方法的分页结果
     * @return 类型：ConditionResVO<E> 对应实体类的 conditionResVO
     */
    public PageResVO<V> getConditionResVO(Page<E> resPage) {
        PageResVO<V> pageResVO = new PageResVO<>();
        pageResVO.setPageNum(resPage.getCurrent());
        pageResVO.setPageSize(resPage.getSize());
        // conditionResVO.setRows(resPage.getRecords());  //conditionResVO返回后再手动添加转为实体类转为VO的数组
        pageResVO.setTotalPages(resPage.getPages());
        pageResVO.setTotalSize(resPage.getTotal());
        return pageResVO;
    }

    public PageResVO<V> getCustomPageResVO(Integer pageNum, Integer pageSize, ArrayList<V> VOList) {
        PageResVO<V> pageResVO = new PageResVO<>();
        pageResVO.setPageNum(pageNum);
        pageResVO.setPageSize(pageSize);
        pageResVO.setTotalPages(VOList.size() % pageSize == 0 ? VOList.size() / pageSize : (VOList.size() / pageSize) + 1);
        pageResVO.setTotalSize(VOList.size());
        if(pageNum * pageSize <= VOList.size()) pageResVO.setRows(VOList.subList((pageNum-1) * pageSize,pageNum * pageSize));
        else pageResVO.setRows(VOList.subList((pageNum-1) * pageSize,VOList.size()));
        //需要注意：subList返回的是原数组的视图，即操作返回的数组会改变原数组。
        return pageResVO;
    }

    //验证pageNum，pageSize
    public void validatePageNumPageSize(Integer pageNum,Integer pageSize) {
        if(pageNum == null) throw new CustomException(ResCodeEnum.BAD_REQUEST,"缺少页码pageNum");
        if(pageSize == null) throw new CustomException(ResCodeEnum.BAD_REQUEST,"缺少页码pageNum");

        if(pageNum <= 0) {
            if(pageSize <= 0) throw new CustomException(ResCodeEnum.BAD_REQUEST,"页码pageNum和每页几条pageSize均需要大于0");
            else throw new CustomException(ResCodeEnum.BAD_REQUEST,"页码pageNum需要大于0");
        }else {
            if(pageSize <= 0) throw new CustomException(ResCodeEnum.BAD_REQUEST,"每页几条pageSize均需要大于0");
        }
    }


}
